package com.xchen.imweb.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author xchen
 * @since 2022-03-14
 */
public class AuthInterceptor implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		HttpSession session = request.getSession(false);
		if (null == session) {
			response.sendRedirect("/login");
			return false;
		}
		Object user = session.getAttribute("user");
		if (null == user) {
			response.sendRedirect("/login");
			return false;
		}
		return true;
	}
}
